package com.wzy.service.reposirory.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wzy.common.base.result.R;
import com.wzy.service.reposirory.dto.NoteView;
import com.wzy.service.reposirory.entity.GlobalWord;
import com.wzy.service.reposirory.entity.UserNote;
import com.wzy.service.reposirory.feign.UserService;
import com.wzy.service.reposirory.service.GlobalWordService;
import com.wzy.service.reposirory.service.UserNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuzhengyu
 * @since 2021-03-13
 */
@RestController
@RequestMapping("/user-note")
public class UserNoteController {

    @Autowired
    UserNoteService userNoteService;

    @Autowired
    UserService userService;

    @Autowired
    GlobalWordService globalWordService;

    @GetMapping("/note")
    public R getNoteByWid(@RequestParam String wid) {
        List<UserNote> list = userNoteService.list(Wrappers.<UserNote>lambdaQuery()
                .eq(UserNote::getWid, wid));
        return R.ok().data("note", list);
    }

    @GetMapping("user/note")
    public R getUserNote(@RequestParam String uid) {
        List<UserNote> list = userNoteService.list(Wrappers.<UserNote>lambdaQuery()
                .eq(UserNote::getUid, uid));
        List<NoteView> collect = list.stream().map(l -> {
            GlobalWord one = globalWordService.getOne(Wrappers.<GlobalWord>lambdaQuery().eq(GlobalWord::getId, l.getWid()));
            NoteView noteView = new NoteView();
            noteView.setId(l.getId());
            noteView.setContent(l.getContent());
            noteView.setGmtCreate(l.getGmtCreate());
            noteView.setWord(one.getSpell());
            return noteView;
        }).collect(Collectors.toList());
        return R.ok().data("notes", collect);
    }

    @DeleteMapping("note")
    public R deleteUserNote(@RequestParam String id) {
        userNoteService.removeById(id);
        return R.ok();
    }


    @PostMapping("/note")
    public R insertNote(@RequestParam String uid,
                        @RequestParam String wid,
                        @RequestParam String uwid,
                        @RequestParam String content) {
        UserNote userNote = new UserNote();
        userNote.setUid(Long.valueOf(uid));
        userNote.setUWId(Long.valueOf(uwid));
        userNote.setWid(Long.valueOf(wid));
        userNote.setContent(content);
        userNote.setGmtCreate(new Date());
        userNote.setGmtModified(new Date());
        userNoteService.save(userNote);
        return R.ok();
    }
}

